package com.xiangwei.util;

import java.io.File;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;

/**
 * 七牛云服务
 * 
 * @author 张攀
 *
 */
public class QiniuUtil {

	private static final String ACCESS_KEY = "Aywm6nIzxloefnziBpPSPjMM4L-LcPBa34r3okYm";
//	private static final String ACCESS_KEY = "2W8mkaTHXuhSWa0JQHwI2xJZNoM3-rC9C5BYOWMP";
//	private static final String SECRET_KEY = "Nw13eNtHjVuTr8kFXWg0HfO23cRh5TU1Tdb3C5Lm";
	private static final String SECRET_KEY = "v95E3Le87dWF2rN08fyMEXGEb-03yzL4RJZdRmqi";
	private static final String BUCKET_NAME = "hechang";
	private static Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
	private static BucketManager bucketManager = new BucketManager(auth);
	private static final Logger logger = LoggerFactory.getLogger(QiniuUtil.class);
	
	public static final String IMAGEURL = "http://7xlvjs.com1.z0.glb.clouddn.com/";     //所有空间图片的访问路径
//	public static final String IMAGEURL = "http://7xk1qf.com1.z0.glb.clouddn.com/";     //所有空间图片的访问路径
	
	/** 
	 * 上传图片到七牛云存储 
	 * @param reader 
	 * @param fileName 
	 * @return String
	 */
	public static String upload(String pathFile, String key) {
		String str = null;
		try {
			UploadManager uploader = new UploadManager();
			String mime = "image/JPG";
			String token = getUpToken();
			Response res = uploader.put(pathFile, key, token, null, mime, false);
			str = res.bodyString();
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return str;
	}
	/** 
	 * 上传图片到七牛云存储 
	 * @param reader 
	 * @param File 
	 * @return String
	 */
	public static String upload(File pathFile, String key) {
		String str = null;
		try {
			UploadManager uploader = new UploadManager();
			String mime = "image/JPG";
			String token = getUpToken();
			Response res = uploader.put(pathFile, key, token, null, mime, false);
			str = res.bodyString();
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return str;
	}
	/** 
	 * 上传图片到七牛云存储 
	 * @param reader 
	 * @param Byte 
	 * @return String
	 */
	public static String upload(byte[] Byte, String key) {
		String str = null;
		try {
			UploadManager uploader = new UploadManager();
			String mime = "image/JPG";
			String token = getUpToken();
			Response res = uploader.put(Byte, key, token, null, mime, false);
			str = res.bodyString();
		} catch (QiniuException e) {
			Response r = e.response;
			logger.error(r.toString());
			logger.error(e.getMessage());
			try {
				logger.error(r.bodyString());
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
		return str;
	}
	
	/**
	 * 上传证书文件
	 * */
	public static String uploadP12(String pathFile,String key){
		String str = null;
		try {
			UploadManager uploader = new UploadManager();
			String mime = "p12";
			String token = getUpToken();
			Response res = uploader.put(pathFile, key, token, null, mime, false);
			str = res.bodyString();
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return str;
	}
	
	
	/**      
	 * 删除七牛云存储上的图片      
	 *  @param key      
	 */     
	public static void delete(String key) {        
		try {
			bucketManager.delete(BUCKET_NAME, key);
		} catch (QiniuException e) {
			logger.error(e.getMessage());
		}
	}
	
	/**
	 * 下载
	 * @param key
	 */
	public static void download(String key){
		String urlSigned = auth.privateDownloadUrl(IMAGEURL+key+".jpg");
		System.out.println(urlSigned);
	}
	// 简单上传，使用默认策略
	private static String getUpToken(){
	    return auth.uploadToken(BUCKET_NAME);
	}
}
